<?php

namespace app\admin\controller\shop\order;

use app\admin\controller\shop\Base;
use think\db\exception\DbException;
use Throwable;

/**
 * 订单评价
 */
class Evaluate extends Base
{
    /**
     * Evaluate模型对象
     * @var object
     * @phpstan-var \app\admin\model\shop\order\Evaluate
     */
    protected object $model;

    protected array|string $preExcludeFields = ['id', 'order_id', 'order_detail_id', 'goods_id', 'goods_name', 'goods_spec', 'score', 'content', 'images', 'user_id', 'user_name', 'user_avatar', 'create_time', 'update_time'];

    protected string|array $quickSearchField = ['user_name'];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new \app\admin\model\shop\order\Evaluate;
        $this->request->filter('clean_xss');
    }


    /**
     * 若需重写查看、编辑、删除等方法，请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
     */

    /**
     * 列表
     * @return void
     * @throws Throwable
     * @throws DbException
     * @noinspection PhpUnusedLocalVariableInspection
     */
    public function index(): void
    {
        if ($this->request->param('select')) {
            $this->select();
        }

        list($where, $alias, $limit, $order) = $this->queryBuilder();

        foreach ($where as $key => $item) {
            list($field, $op, $value) = $item;
            if ($field === 'evaluate.score') {
                $where[] = match ($value) {
                    'bad' => ['score', 'in', [1, 2]],
                    'good' => ['score', 'in', [4, 5]],
                    default => ['score', '=', 3],
                };
                unset($where[$key]);
            }
        }

        $res = $this->model
            ->field($this->indexField)
            ->withJoin($this->withJoinTable, $this->withJoinType)
            ->alias($alias)
            ->where($where)
            ->order($order)
            ->paginate($limit);

        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }
}